//
//  An_CaViewController.m
//  Animation_UIview1
//
//  Created by 刘文超 on 2019/12/24.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "An_CaViewController.h"

@interface An_CaViewController ()
@property(nonatomic,strong)UIView *ls_bgView1;
@property(nonatomic,strong)UIView *ls_bgView2;
@property(nonatomic,strong)UIView *ls_bgView3;
@end

@implementation An_CaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //核心动画介绍
    //@1
    //CAAnimation是所有动画对象的父类，实现CAMediaTiming协议，负责控制动画的时间、速度和时间曲线等等，是一个抽象类，不能直接使用。
    //@2
    //CAPropertyAnimation ：是CAAnimation的子类，它支持动画显示图层的keyPath，一般不直接使用。
    //@3
    //iOS9.0之后新增CASpringAnimation类，它实现弹簧效果的动画，是CABasicAnimation的子类
    //综上，核心动画类中可以直接使用的类有：
    
    //CABasicAnimation
    //　　CAKeyframeAnimation
    //　　CATransition//转场动画
    //　　CAAnimationGroup
    //　　CASpringAnimation
    
    //初始化CAAnimation对象 一般使用animation方法生成实例
    //如果是CAPropertyAnimation的子类，还可以通过animationWithKeyPath生成实例
    
    
    
    //CATransform3D Key Paths : (example)transform.rotation.z
    //rotation.x
    //rotation.y
    //rotation.z
    //rotation 旋轉
    //scale.x
    //scale.y
    //scale.z
    //scale 缩放
    //translation.x
    //translation.y
    //translation.z
    //translation 平移
    
    //CGPoint Key Paths : (example)position.x
    //x
    //y
    
    //CGRect Key Paths : (example)bounds.size.width
    //origin.x
    //origin.y
    //origin
    //size.width
    //size.height
    //size
    
    //opacity
    //backgroundColor
    //cornerRadius
    //borderWidth
    //contents
    
    //Shadow Key Path:
    //shadowColor
    //shadowOffset
    //shadowOpacity
    //shadowRadius
    
    
    CGFloat view_width = ([UIScreen mainScreen].bounds.size.width-100);
    UIView *bgView1=[[UIView alloc]initWithFrame:CGRectMake(50, 100, view_width, 100)];
    [self.view addSubview:bgView1];
    bgView1.backgroundColor= [UIColor redColor];
    self.ls_bgView1 = bgView1;
    
    
    UIView *bgView2=[[UIView alloc]initWithFrame:CGRectMake(50, 200, view_width/2,100)];
    [self.view addSubview:bgView2];
    bgView2.backgroundColor = [UIColor blueColor];;
    self.ls_bgView2 = bgView2;
    
    
    UIView *bgView3=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(bgView2.frame), 200, view_width/2,100)];
    [self.view addSubview:bgView3];
    bgView3.backgroundColor = [UIColor greenColor];;
    self.ls_bgView3 = bgView3;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    switch (self.type) {
        case ANmiationStyle_cabase:
             [self ls_CABaseAnimationposition];
            break;
        case ANmiationStyle_cakey:
            [self ls_CAKeyframAnimation];
            break;
        case ANmiationStyle_transi:
             [self ls_zhuanchangAnimation];
            break;
        case ANmiationStyle_sprin:
             [self ls_SpringAnimation];
            break;
            
        default:
            break;
    } 
    
}

#pragma mark --CABasicAnimation
-(void)ls_CABaseAnimationposition
{
    
    //@1position位置
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
//    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 400)];
//    //fillMode 决定当前对象在非active时间段的行为.比如动画开始之前,动画结束之后
//    //kCAFillModeForwards是在动画开始之后layer迅速进入指定位置开始执行动画并在removedOnCompletion = NO的前提下会停在动画结束的最后位置，而kCAFillModeBackwards则是在动画开始之前迅速进入指定状态并在动画开始后执行动画，动画结束后迅速返回到layer的本身位置
//    animation.fillMode = kCAFillModeForwards;
//    animation.duration = 5;
//    animation.removedOnCompletion = NO;//动画执行完是否还原
//    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//    [self.ls_bgView1.layer addAnimation:animation forKey:@"positionkey"];
    
    //@2rotation
//    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
//    animation.duration = 4;
//    animation.fromValue = 0;
//    animation.toValue = @(M_PI*3/4);
//    animation.removedOnCompletion = NO;//动画执行完是否还原
//    animation.fillMode = kCAFillModeForwards;
//    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
//    [self.ls_bgView1.layer addAnimation:animation forKey:@"rotationkey"];
    
    //@3scale
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 4;
    animation.fromValue = 0;
    animation.toValue = @(1.2);
    animation.removedOnCompletion = NO;//动画执行完是否还原
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [self.ls_bgView1.layer addAnimation:animation forKey:@"scalekey"];

}
#pragma mark - CAKeyframeAnimation
-(void)ls_CAKeyframAnimation
{
    CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.duration = 4.0;
    keyAnimation.removedOnCompletion = NO;
    keyAnimation.fillMode = kCAFillModeForwards;
    keyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    //1.设置value
//    NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(150, 200)];
//    NSValue *value2=[NSValue valueWithCGPoint:CGPointMake(250, 200)];
//    NSValue *value3=[NSValue valueWithCGPoint:CGPointMake(250, 300)];
//    NSValue *value4=[NSValue valueWithCGPoint:CGPointMake(150, 300)];
//    NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(150, 200)];
//    keyAnimation.values = @[value1, value2, value3, value4, value5];
    //2.设置路径  中心点绕路径动
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width-20, [UIScreen mainScreen].bounds.size.height-150));
    keyAnimation.path = path;
    CGPathRelease(path);
    [self.ls_bgView1.layer addAnimation:keyAnimation forKey:@"positionkey"];
}
#pragma mark --- 转场动画
-(void)ls_zhuanchangAnimation
{
    CATransition *animation = [CATransition animation];
//    type的enum值如下：
//    kCATransitionFade 渐变
//    kCATransitionMoveIn 覆盖
//    kCATransitionPush 推出
//    kCATransitionReveal 揭开
    animation.type = kCATransitionPush;//用图片测试比较明显
//    subtype的enum值如下：
//    kCATransitionFromRight 从右边
//    kCATransitionFromLeft 从左边
//    kCATransitionFromTop 从顶部
//    kCATransitionFromBottom 从底部
    animation.subtype = kCAMediaTimingFunctionLinear;
    animation.duration = 2;
    self.ls_bgView2.backgroundColor = [UIColor blackColor];
    [self.ls_bgView2.layer addAnimation:animation forKey:@"transitionkey"];
}
#pragma mark -- CASpringAnimation 弹簧
-(void)ls_SpringAnimation
{
//    mass：质量（影响弹簧的惯性，质量越大，弹簧惯性越大，运动的幅度越大）
//    　　stiffness：弹性系数（弹性系数越大，弹簧的运动越快）
//    　　damping：阻尼系数（阻尼系数越大，弹簧的停止越快）
//    　　initialVelocity：初始速率（弹簧动画的初始速度大小，弹簧运动的初始方向与初始速率的正负一致，若初始速率为0，表示忽略该属性）
//    　　settlingDuration：结算时间（根据动画参数估算弹簧开始运动到停止的时间，动画设置的时间最好根据此时间来设置）
    if (@available(iOS 9.0, *)) {
        CASpringAnimation *animation = [CASpringAnimation animationWithKeyPath:@"bounds"];
        animation.mass = 10.0;
        animation.stiffness = 5000;
        animation.damping = 100.0;
        animation.initialVelocity = 5.f;
        animation.duration = animation.settlingDuration;//估算时间 返回弹簧动画到停止时的估算时间，根据当前的动画参数估算
        animation.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
        animation.fillMode = kCAFillModeForwards;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [self.ls_bgView2.layer addAnimation:animation forKey:@"springkey"];
    } else {
        // Fallback on earlier versions
    }
  
}
@end
